package com.yiren.backstage.communication.analysis;

import android.content.Context;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.SignalSourceConfigBean;
import com.yiren.backstage.config.enums.LocationTypeEnum;
import com.yiren.backstage.currency.bean.GpsInfoBean;
import com.yiren.backstage.currency.enums.GpsStateEnum;
import com.yiren.backstage.currency.utils.GpsUtil;
import com.yiren.backstage.currency.utils.StringUtil;

/**
 * Time:2021/12/23 9:34
 * Author:xiaozan
 * Description:低精度GPSRMC数据
 */
public class GpsRmcDataAnalysis {

    private GpsInfoBean gpsInfo = GpsInfoBean.getInstance();
    private SignalSourceConfigBean signalSourceConfig;

    public final float KPHPerKnot = Float.parseFloat("1.8520");

    public GpsRmcDataAnalysis(Context context) {
        signalSourceConfig = LoadConfigApi.loadSignalSourceConfig(context);
    }

    public void dataAnalysis(String data) {
        if (signalSourceConfig.getLocationType().equals(LocationTypeEnum.EXTERNAL)) return;
        String[] lineArr = data.split(",");
        if (lineArr[0].contains("RMC")) {
            String[] source = lineArr;
            if (null != source && source.length >= 12) {
                if (source[2].equals("A")) {
                    gpsInfo.setExternalLocationState(GpsStateEnum.LOCATION_SUCCESS);
                } else {
                    gpsInfo.setExternalLocationState(GpsStateEnum.LOCATIONING);
                    return;
                }

                GpsInfoBean gcj = GpsUtil.gps84_To_Gcj02(GpsUtil.convertToDecimal(source[3].substring(0, 2) + source[3].substring(2)), GpsUtil.convertToDecimal(source[5].substring(0, 3) + source[5].substring(3)));
                GpsInfoBean bd = GpsUtil.gcj02_To_Bd09(gcj.getLatitude(), gcj.getLongitude());

                if (source[4].length() > 0 && source[3].length() > 2) {
                    gpsInfo.setLatitude(bd.getLatitude());
                } else {
                    gpsInfo.setLatitude(0);
                }

                if (source[6].length() > 0 && source[5].length() > 3) {
                    gpsInfo.setLongitude(bd.getLongitude());
                } else {
                    gpsInfo.setLongitude(0);
                }

                if (source[7].length() > 0) {
                    gpsInfo.setSpeed(Float.parseFloat(source[7]) * KPHPerKnot);
                } else {
                    gpsInfo.setSpeed(0);
                }

                if (source[8].length() > 0) {
                    gpsInfo.setBearing(Float.parseFloat(source[8]));
                } else {
                    gpsInfo.setBearing(0);

                    if (source[10].length() > 0 && source[11].length() > 0) {
                        gpsInfo.setMagnetic(source[11] + source[10]);
                    } else {
                        gpsInfo.setMagnetic("");
                    }

                    if (source.length >= 13) {
                        gpsInfo.setWorkMode(source[12].replaceAll("\r", "").replaceAll("\n", ""));
                    }

                    if (source[9].length() == 6 && source[1].length() >= 6) {
                        String dtString = source[9].substring(4) + source[9].substring(2, 2) + "-" + source[9].substring(0, 2) + " " + source[1].substring(0, 2) + ":" + source[1].substring(2, 2) + ":" + source[1].substring(4);
                        gpsInfo.setUtcDateTime(dtString);
                    }
                }
            } else if (lineArr[0].contains("GGA")) {
                source = lineArr;
                if (null != source && source.length >= 12) {
                    if (!StringUtil.isNothing(source[7])) {
                        gpsInfo.setSatelliteNum(Double.valueOf(source[7]).intValue());
                    } else {
                        gpsInfo.setSatelliteNum(0);
                    }
                }
            }
        }
    }
}
